//
//  RMMAginPasswordViewController.m
//  Romomo
//
//  Created by puman on 2016/10/19.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMAginPasswordViewController.h"
#import "RMMAginPasswordView.h"

@interface RMMAginPasswordViewController ()<UITextFieldDelegate>

@end

@implementation RMMAginPasswordViewController
{
    RMMAginPasswordView *_passView;
    UITextField *_numberT;
    UITextField *_yanT;
    UITextField *_newPasswordT;
    UITextField *_newAginPT;
    NSInteger _numbder;
    NSString *_yanZheng;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCColor;
    self.navigationItem.title = @"重置密码";
    _numbder = 60;
    [self addViews];
    // Do any additional setup after loading the view.
}
- (void)addViews{
    _passView = [[RMMAginPasswordView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight- 64)];
    BUTTON_S(_passView.sendBut, sendButClick);
    BUTTON_S(_passView.button, aginBut);
    [self.view addSubview:_passView];
    for (int i = 0; i < 4; i++) {
        UITextField *textF = [self.view viewWithTag:10 + i];
        textF.delegate = self;
        TEXTFIELD_S(textF, numberTextFieldChang:);
        switch (i) {
            case 0:
            {
                _numberT = textF;
                _numberT.keyboardType =UIKeyboardTypeNumberPad;
                UIButton *button = [RMMFileManager addInputViewForTextField];
                BUTTON_S(button, doneBut);
                _numberT.inputAccessoryView= button;
            }
                break;
            case 1:
            {
                _yanT = textF;
                _yanT.keyboardType =UIKeyboardTypeNumberPad;
                UIButton *button = [RMMFileManager addInputViewForTextField];
                BUTTON_S(button, doneBut);
                _yanT.inputAccessoryView = button;
            }
                break;
            case 2:
                _newPasswordT = textF;
                _newPasswordT.secureTextEntry = YES;
                break;
            case 3:
                _newAginPT = textF;
                _newAginPT.secureTextEntry = YES;
                break;

            default:
                break;
        }
    }
}
- (void)sendButOpen:(UIButton *)button{
    button.backgroundColor = kBasicColor;
    [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    button.userInteractionEnabled = YES;
}
- (void)sendButOff:(UIButton *)button{
    button.backgroundColor = RGBA(238, 238, 238, 1);
    [button setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
    button.userInteractionEnabled = NO;
}
- (void)numberTextFieldChang:(UITextField *)textF{
    if (textF == _numberT) {
        if (textF.text.length >= 11) {
            textF.text = [textF.text substringToIndex:11];
            if([RMMFileManager isPhoneNumberWith:textF.text]) {
                [self sendButOpen:_passView.sendBut];
            } else {
                [RMMFileManager MBProgressWith:@"手机号码输入有误!" onView:self.view];
            }
        } else {
            [self sendButOff:_passView.sendBut];
        }
    }
    if (_numberT.text.length != 0 && _yanT.text.length != 0 && _newPasswordT.text.length != 0 && _newAginPT.text.length != 0) {
        [self butOpen:_passView.button];
    } else {
        [self butOff:_passView.button];
    }
}
// 收取验证码
- (void)sendButClick{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:@"倒计时" repeats:YES];
    [self sendButOff:_passView.sendBut];
    [timer fire];

}
// 发送验证码
- (void)sendMessage{
    if (![RMMFileManager isPhoneNumberWith:_numberT.text]) {
        [RMMFileManager MBProgressWith:@"手机号码输入错误!" onView:self.view];
        return;
    }
    NSString *timersTemp = [RMMFileManager getTimesTamp];
    NSString *sign = [RMMFileManager getMD5WithString:timersTemp];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"sign"] = sign;
    parDic[@"appid"] = APPID;
    parDic[@"timestamp"] = timersTemp;
    parDic[@"mobile"] = _numberT.text;
    NSString *url = SendMessageUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            if ([dataDic[@"message"] isEqualToString:@"验证码发送成功"]) {
                _yanZheng = dataDic[@"data"];
                [self countDown];
            } else {
                [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
            }
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        
    } error:^(NSError *errors) {
        
    } view:self.view];
    
}

// 倒计时
- (void)countDown{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:@"倒计时" repeats:YES];
    [self butOff:_passView.sendBut];
    [timer fire];
    
}
- (void)timer:(NSTimer *)timer{
    if (_numbder == 0) {
        [timer invalidate];
        [_passView.sendBut setTitle:@"收取验证码" forState:(UIControlStateNormal)];;
        _numbder = 60;
    }
    [_passView.sendBut setTitle:[NSString stringWithFormat:@"%ld秒后重发",_numbder] forState:(UIControlStateNormal)];
    _numbder--;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    UITextField *textF = [self.view viewWithTag:textField.tag + 1];
    [textF becomeFirstResponder];
    if (textField.tag == 13) {
        [textField resignFirstResponder];
    }
    return YES;
}
- (void)butOpen:(UIButton *)button{
    button.backgroundColor = kBasicColor;
    [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    button.userInteractionEnabled = YES;
}
- (void)butOff:(UIButton *)button{
    button.backgroundColor = RGBA(238, 238, 238, 1);
    [button setTitleColor:COLORFROMCODE(0xD7D7D7, 1) forState:(UIControlStateNormal)];
    button.userInteractionEnabled = NO;
}

- (void)doneBut{
    [self.view endEditing:YES];
}
//  重置

- (void)aginBut{
    [self.view endEditing:YES];
    if (![_newPasswordT.text isEqualToString:_newAginPT.text]) {
        [RMMFileManager MBProgressWith:@"两次密码输入不一样!" onView:self.view];
        return;
    }
    if (_newPasswordT.text.length < 6 || _newPasswordT.text.length > 20) {
        [RMMFileManager MBProgressWith:@"密码设置长度有误!" onView:self.view];
        return;
    }
    if (!_yanT.text.length) {
        [RMMFileManager MBProgressWith:@"请输入验证码!" onView:self.view];
        return;
    }
    if(![_yanT.text isEqualToString:_yanZheng]){
        [RMMFileManager MBProgressWith:@"验证码输入错误!" onView:self.view];
        return;
    }
    NSString *timersTemp = [RMMFileManager getTimesTamp];
    NSString *sign = [RMMFileManager getMD5WithString:timersTemp];
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"sign"] = sign;
    parDic[@"appid"] = APPID;
    parDic[@"timestamp"] = timersTemp;
    parDic[@"org_no"] = Org_no;
    parDic[@"mobile"] = _numberT.text;
    parDic[@"newpass"] = _newPasswordT.text;
    NSString *url = ForgetPasswordUrl;
    [OSRequestManager requestFWithUrlString:url parDic:parDic method:POST finish:^(NSDictionary *dataDic) {
        [RMMFileManager shareHandle].userDic = dataDic[@"data"];
        NS_POST(@"LANDSUCESS", nil, nil);
        [RMMFileManager MBProgressWith:@"密码重置成功!" onView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    } error:^(NSError *errors) {
        
    } view:self.view];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
